package com.byedu.course.controller;

import com.byedu.course.util.ControllerUtil;
import com.byedu.course.util.UserExportExcelUtil;
import com.byedu.domain.focus.FocusKpiDO;
import com.byedu.domain.focus.vo.enumvos.FocusdepaTypeEnum;
import com.byedu.domain.focus.vo.FocusKpiVO;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.domain.focus.vo.JiduVO;
import com.byedu.focus.*;
import com.byedu.util.CommonUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;


/**
 * Created by Luo on 2017-06-28.
 */
@Controller
@RequestMapping("/ads")
public class AdminAdsController {

    @Autowired
    private FocusProduceService focusProduceService;
    @Autowired
    private FocusUserService focusUserService;
    @Autowired
    private FocusKpiService focusKpiService;
    @Autowired
    private FocusProjectService focusProjectService;
    @Autowired
    private FocusLogService focusLogService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String m = request.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(request, response);
        if(StringUtils.isEmpty(m)){
            m = "ads";
        }
        Calendar a=Calendar.getInstance();
        request.setAttribute("m", m);
        List<FocusKpiVO> kpiList= null;
        FocusKpiDO focusKpiDO = null;
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_1");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_1");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);

        request.setAttribute("firstsnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_2");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_2");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("secondnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_3");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_3");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("thridnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_4");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_4");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("fourthnum",focusKpiDO.getKipnum());
        return "index";
    }
    @RequestMapping("/week")
    public String week(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        List<FocusUserVO> userList = focusUserService.getFocusUserList();
        req.setAttribute("userList", userList);
        if(m.equals("list")){
            String jidu = req.getParameter("jidu");
            String userid = req.getParameter("userid");
            List<FocusKpiVO>  kpiList  = null;
            if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
                if(StringUtil.isNullOrEmpty(userid)) {
                    kpiList  = focusKpiService.getFocusKpiDOWeekByFocusKpiDO(null, CommonUtil.StringToInt(jidu));
                }else {
                    kpiList = focusKpiService.getFocusKpiDOWeekByFocusKpiDO(userid, CommonUtil.StringToInt(jidu));
                }
            }else{
                kpiList = focusKpiService.getFocusKpiDOWeekByFocusKpiDO(adminUser.getId(), CommonUtil.StringToInt(jidu));
            }
            FocusKpiDO focusKpiDO = focusKpiService.getkpiListSum(kpiList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.kpiweekExcelexpot(kpiList, focusKpiDO,adminUser));
            req.setAttribute("kpiList",kpiList);
        }

        return "ads/weekList";
    }
    @RequestMapping("/month")
    public String month(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        List<FocusUserVO> userList = focusUserService.getFocusUserList();
        req.setAttribute("userList", userList);
        if(m.equals("list")){
            String jidu = req.getParameter("jidu");
            String userid = req.getParameter("userid");
            List<FocusKpiVO>  kpiList  = null;
            if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
                if(StringUtil.isNullOrEmpty(userid)) {
                    kpiList  = focusKpiService.getFocusKpiDOMonthByFocusKpiDO(null, CommonUtil.StringToInt(jidu));
                }else {
                    kpiList = focusKpiService.getFocusKpiDOMonthByFocusKpiDO(userid, CommonUtil.StringToInt(jidu));
                }
            }else{
                kpiList = focusKpiService.getFocusKpiDOMonthByFocusKpiDO(adminUser.getId(), CommonUtil.StringToInt(jidu));
            }
            FocusKpiDO focusKpiDO = focusKpiService.getkpiListSum(kpiList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.kpimonthExcelexpot(kpiList, focusKpiDO,adminUser));
            req.setAttribute("kpiList",kpiList);
        }

        return "ads/monthList";
    }
    @RequestMapping("/quater")
    public String quater(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String m = req.getParameter("m");
        String userid = req.getParameter("userid");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> userList = focusUserService.getFocusUserList();
        req.setAttribute("userList", userList);

        if(m.equals("list")){
            String jidu = StringUtil.getNullOrEmpty(req.getParameter("jidu"));
            List<FocusKpiVO>  kpiList  = null;
            if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
                if(StringUtil.isNullOrEmpty(userid)) {
                    kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, jidu);
                }else {
                    kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(userid, jidu);
                }
            }else{
                kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), jidu);
            }
            FocusKpiDO focusKpiDO = focusKpiService.getkpiListSum(kpiList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.kpiquaterExcelexpot(kpiList, focusKpiDO,adminUser));
            req.setAttribute("kpiList",kpiList);
        }

        return "ads/quaterList";
    }
    @RequestMapping("/year")
    public String year(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        List<String> jiduList  = focusKpiService.getyearList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> userList = focusUserService.getFocusUserList();
        req.setAttribute("userList", userList);
        if(m.equals("list")){
            String jidu = req.getParameter("jidu");
            String userid = req.getParameter("userid");
            List<FocusKpiVO>  kpiList  = null;
            if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
                if(StringUtil.isNullOrEmpty(userid)) {
                    kpiList  = focusKpiService.getFocusKpiDOYearByFocusKpiDO(null, jidu);
                } else {
                    kpiList = focusKpiService.getFocusKpiDOYearByFocusKpiDO(userid, jidu);
                }
            }else{
                kpiList = focusKpiService.getFocusKpiDOYearByFocusKpiDO(adminUser.getId(), jidu);
            }
            FocusKpiDO focusKpiDO = focusKpiService.getkpiListSum(kpiList);
            req.setAttribute("sum",focusKpiDO);
            req.setAttribute("downexcel", UserExportExcelUtil.kpiYearExcelexpot(kpiList, focusKpiDO,adminUser));
            req.setAttribute("kpiList",kpiList);
        }
        return "ads/yearList";
    }


}
